package com.bgs.eat.commodity.service.impl;

import com.bgs.eat.commodity.entity.CommodityEntity;
import com.bgs.eat.commodity.entity.CommodityTypeEntity;
import com.bgs.eat.commodity.mapper.CommodityMapper;
import com.bgs.eat.commodity.mapper.CommodityTypeMapper;
import com.bgs.eat.commodity.service.CommodityService;
import com.bgs.eat.commodity.service.CommodityTypeService;
import com.bgs.eat.config.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @ClassName CommodityTypeServiceImpl
 * @Description TODO
 * @Author lzy
 * @Date 2022/10/19 14:11
 **/
@Service
public class CommodityTypeServiceImpl implements CommodityTypeService {

    @Autowired
    CommodityTypeMapper commodityTypeMapper;

    @Autowired
    CommodityMapper commodityMapper;

    /**
     * @Description 新增 商品类别（门户商家版）
     * @Author lzy
     * @Date 2022/10/19 14:15
     * @Return com.bgs.eat.config.CommonResult
     * @param commodityTypeEntity
     */
    @Override
    public CommonResult saveCommodityType(CommodityTypeEntity commodityTypeEntity) {
        commodityTypeEntity.setDelFlag(0);
        commodityTypeEntity.setCreateTime(new Date());
        int insert = commodityTypeMapper.insert(commodityTypeEntity);
        if(insert > 0) {
            return CommonResult.ok(insert);
        } else {
            return CommonResult.error("500");
        }
    }

    /**
    * @Description 根据店铺id获取当前 商品分类列表信息（门户商家版）
    * @Author lzy
    * @Date 2022/10/19 15:22
    * @Return com.bgs.eat.config.CommonResult
    * @param shopId
    */
    @Override
    public CommonResult getCommodityTypeList(Integer shopId) {
        CommodityTypeEntity commodityTypeEntity = new CommodityTypeEntity();
        commodityTypeEntity.setShopId(shopId);
        List<CommodityTypeEntity> commodityTypeEntities = commodityTypeMapper.select(commodityTypeEntity);
        return CommonResult.ok(commodityTypeEntities);
    }

    /**
    * @Description 修改商品类型（门户商家版）
    * @Author lzy
    * @Date 2022/10/20 9:20
    * @Return com.bgs.eat.config.CommonResult
    * @param commodityTypeEntity
    */
    @Override
    public CommonResult editCommodityType(CommodityTypeEntity commodityTypeEntity) {
        commodityTypeEntity.setUpdateTime(new Date());
        int i = commodityTypeMapper.updateByPrimaryKeySelective(commodityTypeEntity);
        if (i > 0) {
            return CommonResult.ok(i);
        } else {
            return CommonResult.error("500");
        }
    }

    /**
    * @Description 删除商品类别（门户商家版）
    * @Author lzy
    * @Date 2022/10/20 9:25
    * @Return com.bgs.eat.config.CommonResult
    * @param id
    */
    @Override
    public CommonResult deleteCommodityType(Integer id) {
        CommodityTypeEntity commodityTypeEntity = new CommodityTypeEntity();
        commodityTypeEntity.setId(id);
        int delete = commodityTypeMapper.delete(commodityTypeEntity);
        if (delete > 0) {
            return CommonResult.ok(delete);
        } else {
            return CommonResult.error("500");
        }
    }

    /**
    * @Description 校验 商品类别是否含有 商品信息（门户商家版）
    * @Author lzy
    * @Date 2022/10/25 11:27
    * @Return com.bgs.eat.config.CommonResult
    * @param id
    */
    @Override
    public CommonResult checkTypeHaveCommodity(Integer id) {
        CommodityEntity commodityEntity = new CommodityEntity();
        commodityEntity.setTypeId(id);
        int i = commodityMapper.selectCount(commodityEntity);
        if (i <= 0) {
            return CommonResult.ok(i);
        } else {
            return CommonResult.error("500");
        }
    }
}
